package com.xtr.tx.config.hikari;

import com.lcn.core.db.LCNDataSourceProxy;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import tk.mybatis.spring.annotation.MapperScan;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;

/**
 * <p>类说明</p>
 *
 * @author 张峰 zfvip_it@163.com
 * @createTime: 2018-11-28 11:10
 */
@Configuration
@MapperScan(basePackages = {HikariConfig.PACKAGE_WRITER, HikariConfig.PACKAGE_READER})
public class HikariConfig {

    static final String PACKAGE_WRITER = "com.xtr.**.writer";
    static final String PACKAGE_READER = "com.xtr.**.reader";


    @Resource
    private Environment environment;

    /**
     * 创建dataSource对象
     *
     * @return
     */
    @Bean(name = "writerDataSource")
    @Primary
    public DataSource dataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(environment.getProperty("writer.jdbc.driverClassName"));
        dataSource.setJdbcUrl(environment.getProperty("writer.jdbc.url"));
        dataSource.setUsername(environment.getProperty("writer.jdbc.username"));
        dataSource.setPassword(environment.getProperty("writer.jdbc.password"));
        dataSource.setMinimumIdle(5);
        dataSource.setMaximumPoolSize(30);
        dataSource.setAutoCommit(true);
        dataSource.setIdleTimeout(30000);
        dataSource.setPoolName("DatebookHikariCP");
        dataSource.setMaxLifetime(60000);
        dataSource.setConnectionTimeout(30000);
        dataSource.setConnectionTestQuery("SELECT 1");
        dataSource.setTransactionIsolation(String.valueOf(Connection.TRANSACTION_READ_COMMITTED));
        LCNDataSourceProxy proxy = new LCNDataSourceProxy();
        //数据源
        proxy.setDataSource(dataSource);
        //tx最大连接
        proxy.setMaxCount(30);
        return proxy;
    }

    /**
     * 创建dataSource对象
     *
     * @return
     */
    @Bean(name = "readerDataSource")
//    @Primary
    public DataSource dataSource1() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(environment.getProperty("writer.jdbc.driverClassName"));
        dataSource.setJdbcUrl(environment.getProperty("writer.jdbc.url"));
        dataSource.setUsername(environment.getProperty("writer.jdbc.username"));
        dataSource.setPassword(environment.getProperty("writer.jdbc.password"));
        dataSource.setMinimumIdle(5);
        dataSource.setMaximumPoolSize(10);
        dataSource.setAutoCommit(true);
        dataSource.setIdleTimeout(30000);
        dataSource.setPoolName("DatebookHikariCP");
        dataSource.setMaxLifetime(60000);
        dataSource.setConnectionTimeout(30000);
        dataSource.setConnectionTestQuery("SELECT 1");
        dataSource.setTransactionIsolation(String.valueOf(Connection.TRANSACTION_READ_COMMITTED));
        return dataSource;
    }
}
